package com.youlu.campus.web.university.service.impl;

import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.university.dto.DicQueryRequest;
import com.youlu.campus.web.university.dto.DictionaryResponse;
import com.youlu.campus.web.university.entity.DictionaryInfo;
import com.youlu.campus.web.university.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 字典管理接口实现类
 * @author: YangJ
 * @date: 2020/12/10 13:50
 */
@Service("dictionaryService")
public class DictionaryServiceImpl extends BaseService implements DictionaryService {

    @Override
    public List<DictionaryResponse> queryMajor(DicQueryRequest request) {

        Query query = new Query();
        if(StringUtils.isNotBlank(request.getKey())){
            Pattern pattern = Pattern.compile("^.*" + request.getKey() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("name").regex(pattern));
        }
        query.addCriteria(Criteria.where("type").is(1));
        List<DictionaryInfo> list = mongoTemplate.find(query, DictionaryInfo.class);
        List<DictionaryResponse> majorList = list.stream().map(dictionaryInfo -> {
            DictionaryResponse response = mapperFacade.map(dictionaryInfo, DictionaryResponse.class);
            return response;
        }).collect(Collectors.toList());

        return majorList;
    }

    @Override
    public List<DictionaryResponse> labelList() {
        Query query = new Query();
        query.addCriteria(Criteria.where("type").is(2));
        List<DictionaryInfo> list = mongoTemplate.find(query, DictionaryInfo.class);
        List<DictionaryResponse> labelList = list.stream().map(dictionaryInfo -> {
            DictionaryResponse response = mapperFacade.map(dictionaryInfo, DictionaryResponse.class);
            return response;
        }).collect(Collectors.toList());

        return labelList;
    }
}
